package com.hertz.iris.core;

import java.io.IOException;

import javax.inject.Inject;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;

import com.hertz.iris.IrisServiceProvider;
import com.hertz.iris.pojo.IrisVersion;
import com.hertz.iris.util.Ln;

public class VersionService {
	@Inject IrisServiceProvider serviceProvider;
	@Inject
	public VersionService() {
	}

	/**
	 * 获取当前版本号
	 * 
	 * @param context
	 * @return
	 */
	public int getVerCode(Context context) {
		int verCode = -1;
		try {
			// 获取packagemanager的实例
			PackageManager packageManager = context.getPackageManager();
			// getPackageName()是你当前类的包名，0代表是获取版本信息
			PackageInfo packInfo = packageManager.getPackageInfo(
					context.getPackageName(), 0);
			verCode = packInfo.versionCode;
			// verCode =
			// context.getPackageManager().getPackageInfo(
			// "com.example.update", 0).versionCode;
			System.out.println("nowcode---?>>>" + verCode);
		} catch (NameNotFoundException e) {
			Ln.e(e, "no version code");
		}
		return verCode;
	}

	/**
	 * 获取版本名称
	 * 
	 * @param context
	 * @return
	 */
	public String getVerName(Context context) {
		String verName = "";

		try {
			// 获取packagemanager的实例
			PackageManager packageManager = context.getPackageManager();
			// getPackageName()是你当前类的包名，0代表是获取版本信息
			PackageInfo packInfo = packageManager.getPackageInfo(
					context.getPackageName(), 0);
			verName = packInfo.versionName;
			// verName =
			// context.getPackageManager().getPackageInfo(
			// "com.example.update", 0).versionName;
		} catch (NameNotFoundException e) {
			Ln.e(e, "no version name");
		}
		return verName;
	}

	/**
	 * 获取服务器的版本
	 * 
	 * @return
	 */
	public IrisVersion getServerVerCode(Activity activity) throws IOException{
		return serviceProvider.getService(activity).getVersion();
	}
	
	public IrisVersion checkVersion(Activity activity) throws IOException{
		int verCode = getVerCode(activity);
		IrisVersion irisVersion = getServerVerCode(activity);
		if(irisVersion != null && irisVersion.getVersionCode() - verCode > 0){
			return irisVersion;
		}else{
			return null;
		}
		
	}
	
	public void showNoticeDialog(final Context mContext,final IrisVersion irisVersion)  
    {  
		
        AlertDialog.Builder builder = new Builder(mContext);
        builder.setTitle("软件版本更新");  
        builder.setMessage(irisVersion.getDes());  
        builder.setPositiveButton("下载", new DialogInterface.OnClickListener() {  
  
            @Override  
            public void onClick(DialogInterface dialog, int which)  
            {  
                dialog.dismiss();  
                Uri uri = Uri.parse(irisVersion.getDownloadPath()); 

                Intent intent =new Intent(Intent.ACTION_VIEW, uri);
                mContext.startActivity(intent);
            }  
        });  
        builder.setNegativeButton("以后再说", new DialogInterface.OnClickListener()  
        {  
  
            @Override  
            public void onClick(DialogInterface dialog, int which)  
            {  
                dialog.dismiss();  
            }  
        });  
        Dialog noticeDialog = builder.create();  
        noticeDialog.show();  
    }  
	
}
